package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.CareRecordMapper;
import com.tjetc.dao.CaregiverInfoMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.entity.CareRecord;
import com.tjetc.entity.CaregiverInfo;
import com.tjetc.entity.ElderInfo;
import com.tjetc.entity.Employee;
import com.tjetc.service.CareRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class CareRecordServiceImpl implements CareRecordService {
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Autowired
    private CaregiverInfoMapper caregiverInfoMapper;
    @Autowired
    private CareRecordMapper careRecordMapper;
    @Override
    public JsonResult findById(Long id) {
        CareRecord careRecord=careRecordMapper.selectById(id);
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        ElderInfo elderInfo = elderInfoMapper.selectById(careRecordMapper.selectElderlyId(id));
        CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(careRecordMapper.selectCaregiverId(id));
        careRecord.setElderly(elderInfo);
        careRecord.setCaregiver(caregiverInfo);
        return JsonResult.success(careRecord);
    }
    @Override
    public JsonResult findPageElderName(String name, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<CareRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        //护工查所有分页，没有模糊查询
        Page<CareRecord>careRecordPage= careRecordMapper.selectPageLikeElderName(page,name);
        for (CareRecord careRecord : careRecordPage.getRecords()) {
            ElderInfo elderInfo = elderInfoMapper.selectById(careRecordMapper.selectElderlyId(careRecord.getCareId()));
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(careRecordMapper.selectCaregiverId(careRecord.getCareId()));
            careRecord.setElderly(elderInfo);
            careRecord.setCaregiver(caregiverInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(careRecordPage);
    }
    @Override
    public JsonResult findPageCaregiverName(String name, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<CareRecord> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        //护工查所有分页，没有模糊查询
        Page<CareRecord>careRecordPage= careRecordMapper.selectPageLikeCaregiverName(page,name);
        for (CareRecord careRecord : careRecordPage.getRecords()) {
            ElderInfo elderInfo = elderInfoMapper.selectById(careRecordMapper.selectElderlyId(careRecord.getCareId()));
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(careRecordMapper.selectCaregiverId(careRecord.getCareId()));
            careRecord.setElderly(elderInfo);
            careRecord.setCaregiver(caregiverInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(careRecordPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=careRecordMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(CareRecord careRecordParam) {
        CareRecord careRecord=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(careRecordParam.getCareId()==null||careRecordParam.getCareId()<=0){
            //新增
            careRecord=new CareRecord();
            BeanUtils.copyProperties(careRecordParam,careRecord);
            careRecord.setCreateTime(now);
            careRecord.setUpdateTime(now);
            careRecordMapper.insert(careRecord);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            careRecord=careRecordMapper.selectById(careRecordParam.getCareId());
            if(careRecordParam==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            careRecord.setUpdateTime(now);
            careRecord.setCareContent(careRecordParam.getCareContent());
            careRecord.setCaregiver(careRecordParam.getCaregiver());
            careRecord.setElderly(careRecordParam.getElderly());
            careRecord.setCareTime(careRecordParam.getCareTime());
            careRecord.setCareType(careRecordParam.getCareType());
            careRecord.setEvaluation(careRecordParam.getEvaluation());
            careRecord.setItemId(careRecordParam.getItemId());
            careRecord.setRemark(careRecordParam.getRemark());
            careRecord.setStatus(careRecordParam.getStatus());
            careRecord.setIsDeleted(careRecordParam.getIsDeleted());
            careRecord.setCaregiverId(careRecordParam.getCaregiverId());
            careRecord.setElderlyId(careRecordParam.getElderlyId());
            int rows=careRecordMapper.updateById(careRecord);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
